package com.socialuni.center.web.serive.comment;

import com.socialuni.sdk.api.model.QO.comment.UniCommentPostQO;
import com.socialuni.sdk.api.model.RO.comment.TalkCommentRO;
import com.socialuni.center.web.factory.CenterCommentROFactory;
import com.socialuni.center.web.utils.CenterUserUtil;
import com.socialuni.center.web.utils.UnionIdDbUtil;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.model.QO.comment.CommentAddVO;
import com.socialuni.sdk.repository.CommentRepository;
import com.socialuni.sdk.repository.NotifyRepository;
import com.socialuni.sdk.repository.TalkRepository;
import com.socialuni.sdk.service.NotifyService;
import com.socialuni.sdk.service.ReportService;
import com.socialuni.sdk.service.ViolationService;
import com.socialuni.sdk.service.comment.SocialPostCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.EntityManager;

/**
 * @author qinkaiyuan
 * @date 2020-05-16 20:11
 */
@Service
@Slf4j
public class CenterCommentService {

    @Resource
    private EntityManager entityManager;

    @Resource
    private CommentRepository commentRepository;

    @Resource
    private TalkRepository talkRepository;

    @Resource
    private NotifyService notifyService;
    @Resource
    private NotifyRepository notifyRepository;
    @Resource
    private ViolationService violationService;
    @Resource
    private ReportService reportService;
    @Resource
    SocialPostCommentService socialPostCommentService;


    public TalkCommentRO postComment(UniCommentPostQO addVO) {
        UserDO user = CenterUserUtil.getMineUser();
        //推送消息
//        notifyService.sendNotifies(comment.getNotifies());
        //因前台问题，无论如何都返回父级comment
        /*if (comment.getParentComment() != null) {
            comment = commentRepository.findById(comment.getParentComment().getId()).get();
        }*/
        CommentAddVO commentAddVO = new CommentAddVO();

        commentAddVO.setContent(addVO.getContent());

        Integer talkIdResult = UnionIdDbUtil.getTalkIdByUnionId(addVO.getTalkId());
        commentAddVO.setTalkId(talkIdResult);

        String commentId = addVO.getCommentId();
        //不为null，才查询
        if (commentId != null) {
            Integer commentIdResult = UnionIdDbUtil.getCommentIdByUid(commentId);
            commentAddVO.setCommentId(commentIdResult);

            String replyCommentId = addVO.getReplyCommentId();
            if (replyCommentId != null) {
                Integer replyCommentIdResult = UnionIdDbUtil.getCommentIdByUid(replyCommentId);
                commentAddVO.setReplyCommentId(replyCommentIdResult);
            }
        }

        TalkCommentRO commentRO = socialPostCommentService.addComment(commentAddVO, user, addVO.getThreeUnionId());

        return CenterCommentROFactory.newHomeCommentRO(commentRO);
    }

}
